//
//  MKWithdrawalView.m
//  MKProject
//
//  Created by jkkj on 2021/4/26.
//

#import "MKWithdrawalView.h"
@interface MKWithdrawalView ()
@property (nonatomic, strong)UIView * indicatorView;
@property (nonatomic, strong)UIScrollView * scrollerView;
@property (nonatomic, strong) NSMutableArray<UIButton *> *itemBtnArr;
@end

@implementation MKWithdrawalView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.lineColor = kColorMain;
        self.selectFont = KCFont(17);
        self.noteFont = KFont(13);
        self.selectColor = [UIColor blackColor];
        self.noteColor = [UIColor blackColor];
        self.itemBtnArr = [[NSMutableArray alloc] init];
        self.typeUI = MKUITypeDefault;
    }
    return self;
}

- (UIScrollView *)scrollerView{
    if (!_scrollerView) {
        _scrollerView = [[UIScrollView alloc] initWithFrame:Frame(0, 0, self.width, self.height)];
        [self addSubview:_scrollerView];
    }
    return _scrollerView;
}
- (void)setLineColor:(UIColor *)lineColor{
    _lineColor = lineColor;
}

- (NSMutableArray<UIButton *>*)itemBtnArr
{
    if (!_itemBtnArr) {
        _itemBtnArr = [[NSMutableArray alloc]init];
    }
    return _itemBtnArr;
}

- (UIView *)indicatorView
{
    if (!_indicatorView) {
        _indicatorView = [[UIView alloc]init];
        _indicatorView.height = 4;
        _indicatorView.width = 30;
        _indicatorView.top = self.height-5;
        _indicatorView.backgroundColor = _lineColor;
        [self addSubview:_indicatorView];
        setViewCorner(_indicatorView, 2);
    }
    return _indicatorView;
}

- (void)setSelectIndex:(NSInteger)selectIndex{
    _selectIndex = selectIndex;
    for (int i=0; i<self.itemBtnArr.count; i++) {
        UIButton *btn = self.itemBtnArr[i];
        if (selectIndex == btn.tag) {
            btn.selected = YES;
            btn.titleLabel.font = self.selectFont;
            WeakSelf;
            [UIView animateWithDuration:0.25 animations:^{
                wself.indicatorView.centerX = btn.centerX;
            }];
        }else{
            btn.selected = NO;
            btn.titleLabel.font = self.noteFont;
        }
    }
}
#pragma mark --Setter

- (void)setTitleArray:(NSArray *)titleArray
{
//    NSArray *titlesArr = @[@"提现",@"兑换"];
    _titleArray = titleArray;
    [self removeAllSubviews];
    UIButton *curBtn = nil;
    CGFloat width = kScreenWidth/titleArray.count;
    for (int i=0; i<titleArray.count; i++) {
        NSString *title = titleArray[i];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:self.noteColor forState:UIControlStateNormal];
        [btn setTitleColor:self.selectColor forState:UIControlStateSelected];
        btn.titleLabel.font = self.noteFont;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        if (self.typeUI == MKUITypeScroller) {
            CGFloat btnWidth = [Common getStringWidthWithText:title font:self.selectFont viewHeight:self.height] + 15;
            btn.frame = Frame(curBtn.right,0,btnWidth, self.height);
            [self.scrollerView addSubview:btn];
        }else{
            btn.frame = Frame(i*width,0,width, self.height);
            [self addSubview:btn];
        }
        curBtn = btn;
        [self.itemBtnArr addObject:btn];
    }
    self.selectIndex = 0;
}

- (void)btnClick:(UIButton *)sender{
    self.selectIndex = sender.tag;
    if ([self.delegate respondsToSelector:@selector(switchIndex:)]) {
            [self.delegate switchIndex:sender.tag];
        }
    if (self.switchBlock) {
        self.switchBlock(sender.tag);
    }
}

@end
